﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using System.Text.RegularExpressions;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 30060_游戏商店支付请求
	/// </summary>
	public class Action30060 : BaseAction
	{
		private int tableId;
		private int userid;
		private int pid;
		private string orderid;

		/// <summary>
		/// 返回结果
		/// </summary>
		private bool result = false;
		/// <summary>
		/// 返回总金币数量
		/// </summary>
		private int gold = 0;

		public Action30060(HttpGet actionGetter)
			: base((short)ActionType.DZShopPayRequest, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("UserID", ref userid) &&
				httpGet.GetInt("PId", ref pid) &&
				httpGet.GetString("OrderId", ref orderid))  //这里是苹果单号校验
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			var _currentGameUser = GameTable.Current.GetUser(userid);
			if (_currentGameUser == null)
			{
				Tool.WriteWarmingLine("消息30060玩家不存在");
				return false;
			}
			var shopinfo = new ShareCacheStruct<ShopInfo>().FindAll().First(i => i.ShopID == pid);
			if (shopinfo == null)
			{
				Tool.WriteErrorLine(string.Format("消息30060商品Id:{0}不存在", pid));
				return false;
			}
			if (string.IsNullOrEmpty(this.orderid))
			{
				Tool.WriteErrorLine("30060申请购买请求订单号为空");
				return false;
			}
			//Regex reg = new Regex(@"/^[A-Za-z0-9]+$/");
			//if (!reg.IsMatch(this.orderid))
			//{
			//	Tool.WriteErrorLine("30060申请购买请求订单号不正确，不是字母+数字规则");
			//	return false;
			//}
			_currentGameUser.GameCoin += shopinfo.GameCoin;
			this.gold = _currentGameUser.GameCoin;
			this.result = true;
			return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(this.result);
			this.PushIntoStack(this.gold);
		}
	}
}
